package com.javaPro.myProject.modules.comment.service.impl;

import com.javaPro.myProject.modules.article.dao.TArticleDao;
import com.javaPro.myProject.modules.article.entity.TArticle;
import com.javaPro.myProject.modules.comment.dao.TCommentDao;
import com.javaPro.myProject.modules.comment.entity.TComment;
import com.javaPro.myProject.modules.comment.service.TCommentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 评论表(TComment)表服务实现类
 *
 * @author makejava
 * @since  16:35:52
 */
@Service("tCommentService")
public class TCommentServiceImpl implements TCommentService {
    @Resource
    private TCommentDao tCommentDao;
    @Resource
    private TArticleDao articleDao;
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public TComment queryById(Integer id) {
        return this.tCommentDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param tComment 筛选条件
     * @return 查询结果
     */
    @Override
    public List<TComment> queryByPage(TComment tComment) {
        return this.tCommentDao.queryAllByLimit(tComment);
    }

    /**
     * 新增数据
     *
     * @param tComment 实例对象
     * @return 实例对象
     */
    @Override
    public int insert(TComment tComment) {
        Integer articleId = tComment.getArticleId();
        TArticle tArticle = articleDao.queryById(articleId);
        Integer authorId = tArticle.getAuthorId();
        if (tComment.getResFlag() == null || !"Y".equals(tComment.getResFlag())){//如果不是回复，是第一次评论的时候
            if (authorId.equals(tComment.getSenderId())){
                return -1;
            }
            tComment.setRecriverId(authorId);
        }
        int insert = this.tCommentDao.insert(tComment);
        return insert;
    }

    /**
     * 修改数据
     *
     * @param tComment 实例对象
     * @return 实例对象
     */
    @Override
    public TComment update(TComment tComment) {
        this.tCommentDao.update(tComment);
        return this.queryById(tComment.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.tCommentDao.deleteById(id) > 0;
    }
}
